package org.wyl.xiachufang.spider;

import cn.hutool.core.util.StrUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Component;
import org.wyl.xiachufang.entity.FoodEntity;
import org.wyl.xiachufang.entity.MenuEntity;
import org.wyl.xiachufang.entity.MenuFoodEntity;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 吴宇亮 on 2022/7/25 16:39
 */
@Component
public class FoodSpider implements PageProcessor {

    private static final Site site = Site.me().setTimeOut(30000).setRetryTimes(3).setSleepTime(1000);

    /**
     * Site就是要分析那个页面 一般是拿当前页面
     */
    @Override
    public Site getSite() {
        return site;
    }

    @Override
    public void process(Page page) {
        Boolean isVaild = false; // 是否是合法的数据

        // 主页面
        String pageUrl = page.getUrl().toString();
        if (pageUrl.startsWith("https://mip.xiachufang.com/category") || pageUrl.startsWith("https://hanwuji.xiachufang.com/category/")) {
            List<String> aTags = page.getHtml().css("#add-more-container a").all();
            for (String aTag : aTags) {
                String href = Jsoup.parse(aTag).select("a").attr("href");
                // 获取子页面
                page.addTargetRequest(href);
            }
        }
        // 菜单页面
        else if(pageUrl.startsWith("https://hanwuji.xiachufang.com/recipe")){
            isVaild = true;
            List<MenuEntity> menuEntities = new ArrayList<>();
            List<FoodEntity> foodEntities = new ArrayList<>();
            List<MenuFoodEntity> menuFoodEntities = new ArrayList<>();

            String $menuName = page.getHtml().css("#__layout > div > div > div > div > div.recipe-main > div.recipe-body > header > h1").get();
            String menuName = Jsoup.parse($menuName).text();

            String $desc = page.getHtml().css("#__layout > div > div > div > div > div.recipe-main > div > section.recipe-desc").get();
            String description = Jsoup.parse($desc).select(".recipe-desc").text();

            MenuEntity menuEntity = new MenuEntity();
            menuEntity.setMenuName(menuName);
            menuEntity.setUrl(pageUrl);
            menuEntity.setDescription(description);
            menuEntities.add(menuEntity);

            String imgTag = page.getHtml().css("#__layout > div > div > div > div > div.recipe-main > a > div.recipe-cover.aspect-ratio.aspect-ratio--6x4.cover.bg-center > mip-img").get();
            if(StrUtil.isNotBlank(imgTag)) {
                String memuImageSrc = Jsoup.parse(imgTag).select("mip-img").attr("src");
                page.putField("memuImageSrc", memuImageSrc); // 菜的图片
            }
            page.putField("menuEntities", menuEntities);

            // 抓取菜单-食材配单表
            String table = page.getHtml().css("#ings > div").get();
            for (Element aTag : Jsoup.parse(table).select("a")) {
                String foodName = aTag.select(".ing-name").text();
                String amount = aTag.select(".ing-amount").text();
                if(StrUtil.isBlank(amount)) {
                    isVaild = false;
                }

                FoodEntity foodEntity = new FoodEntity();
                foodEntity.setFoodName(foodName);
                foodEntities.add(foodEntity);

                MenuFoodEntity menuFoodEntity = new MenuFoodEntity();
                menuFoodEntity.setMenuName(menuName);
                menuFoodEntity.setFoodName(foodName);
                menuFoodEntity.setFoodAmount(amount);
                menuFoodEntities.add(menuFoodEntity);
            }
            //System.out.println("foods: " + JSON.toJSONString(foodEntities));
            page.putField("foodEntities", foodEntities);

            //System.out.println("menuFoodEntities: " + JSON.toJSONString(menuFoodEntities));
            page.putField("menuFoodEntities", menuFoodEntities);
        }

        page.putField("isVaild", isVaild);
    }
}
